﻿using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace EmoneyWeiXinBLL
{
    public class GetConentByCodeRule
    {
        public string getKey(string key)
        {
            return "yicai" + key;
        }

        public List<Security> GetAll()
        {
            MemcachedTools1 cache = new MemcachedTools1();
            List<Security> list = new List<Security>();
            WeiXinReplyRule dal = new WeiXinReplyRule();
            list = cache.Get<List<Security>>(getKey("Security"));
            if (list == null || list.Count == 0)
            {
                list = dal.GetAllStocks();
                cache.Add(getKey("Security"), list, 6, 0, 0);
            }
            return list;
        }

        public string strcode = "";

        public string strcodename = "";

        public string ReplaceGP(string str)
        {
            str = " " + str;
            str = SbCodeReg.Replace(str, CapCode);
            if (strcodename != "")
            {
                return strcodename;
            }
            str = SbNameReg.Replace(str, CapName);
            if (strcode != "")
            {
                return strcode;
            }
            return "";
        }
        public string ContentTozhContent(string content)
        {
            try
            {
                string str = ReplaceGP(content);

                return str;
            }
            catch (Exception)
            {
                return "";
            }
        }

        private Regex SbCodeReg
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                MemcachedTools1 Cache = new MemcachedTools1();
                Regex sbCodeReg = Cache.Get<Regex>(getKey("sbCodeReg"));
                if (sbCodeReg != null)
                    return sbCodeReg;
                List<Security> list = GetAll();
                for (int i = 0; i < list.Count; i++)
                {
                    try
                    {
                        string sName = list[i].SecuName;
                        string code = list[i].SecuCode;
                        if (code.Length != 6)
                        {
                            continue;
                        }
                        if (string.IsNullOrEmpty(sName))
                        {
                            continue;
                        }
                        sb.Append("[^#]").Append(code).Append("|");
                    }
                    catch
                    {

                    }
                }
                sb = sb.Remove(sb.Length - 1, 1);
                string str = sb.ToString();
                Regex reg = new Regex(str);
                Cache.Add(getKey("sbCodeReg"), reg, 6, 0, 0);
                return reg;
            }
        }


        private Regex SbNameReg
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                MemcachedTools1 Cache = new MemcachedTools1();
                Regex sbCodeReg = Cache.Get<Regex>(getKey("sbNameReg"));
                if (sbCodeReg != null)
                    return sbCodeReg;

                List<Security> list = GetAll();
                for (int i = 0; i < list.Count; i++)
                {
                    try
                    {
                        string sName = list[i].SecuName;
                        string code = list[i].SecuCode;
                        if (code.Length != 6)
                        {
                            continue;
                        }
                        if (string.IsNullOrEmpty(sName))
                        {
                            continue;
                        }
                        sb.Append(sName.Replace("*", "\\*")).Append("|");

                    }
                    catch
                    {

                    }
                }

                sb.Append("万科A").Append("|");
                sb = sb.Remove(sb.Length - 1, 1);
                string str = sb.ToString();
                Regex reg = new Regex(str);
                Cache.Add(getKey("sbNameReg"), reg, 6, 0, 0);
                return reg;
            }
        }

        public string CapCode(Match m)
        {
            string x = m.ToString();
            x = x.Substring(1, 6);
            // string pre = m.Value.Substring(0, 1);
            if (strcodename == "")
                strcodename = x;
            return x;//pre + "<a href='javascript:void(0)' style='color:#2db2eb;' title='" + x + "'>" + x + "</a>";
        }

        public string CapName(Match m)
        {
            GetConentByCodeRule secbll = new GetConentByCodeRule();

            string c = m.ToString().Replace("A", "Ａ").ToString();

            string code = secbll.GetAll().Where(x => x.SecuName == c.ToString()).ToList()[0].SecuCode;
            if (strcode == "")
                strcode += code;

            return code;// "<a href='javascript:void(0)' style='color:#2db2eb;' title='" + code + "'>" + m.ToString() + "</a>"; ;
        }
    }
}
